package com.xs.business.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.lang.UUID;
import com.xs.business.domain.TOrderFrom;
import com.xs.business.mapper.TOrderFromMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xs.business.mapper.TSkuMapper;
import com.xs.business.domain.TSku;
import com.xs.business.service.ITSkuService;

/**
 * 商品管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-13
 */
@Service
public class TSkuServiceImpl implements ITSkuService 
{
    @Autowired
    private TSkuMapper tSkuMapper;
    @Autowired
    private TOrderFromMapper tOrderFromMapper;

    /**
     * 查询商品管理
     * 
     * @param skuId 商品管理主键
     * @return 商品管理
     */
    @Override
    public TSku selectTSkuBySkuId(Integer skuId)
    {
        return tSkuMapper.selectTSkuBySkuId(skuId);
    }

    /**
     * 查询商品管理列表
     * 
     * @param tSku 商品管理
     * @return 商品管理
     */
    @Override
    public List<TSku> selectTSkuList(TSku tSku)
    {
        return tSkuMapper.selectTSkuList(tSku);
    }

    /**
     * 新增商品管理
     * 
     * @param tSku 商品管理
     * @return 结果
     */
    @Override
    public int insertTSku(TSku tSku)
    {
        return tSkuMapper.insertTSku(tSku);
    }

    /**
     * 修改商品管理
     * 
     * @param tSku 商品管理
     * @return 结果
     */
    @Override
    public int updateTSku(TSku tSku)
    {
        return tSkuMapper.updateTSku(tSku);
    }

    /**
     * 批量删除商品管理
     * 
     * @param skuIds 需要删除的商品管理主键
     * @return 结果
     */
    @Override
    public int deleteTSkuBySkuIds(Integer[] skuIds)
    {
        return tSkuMapper.deleteTSkuBySkuIds(skuIds);
    }

    /**
     * 删除商品管理信息
     * 
     * @param skuId 商品管理主键
     * @return 结果
     */
    @Override
    public int deleteTSkuBySkuId(Integer skuId)
    {
        return tSkuMapper.deleteTSkuBySkuId(skuId);
    }

    @Override
    public void createOrder(TSku tSku) {
        // 查看是否存在 userid 和  skuid  type
        TOrderFrom query = new TOrderFrom();
        query.setSkuId(Long.valueOf(tSku.getSkuId()));
        query.setUserId(tSku.getUserId());
        query.setType(1);
        List<TOrderFrom> tOrderFroms = tOrderFromMapper.selectTOrderFromList(query);
        if (!tOrderFroms.isEmpty()) {
            query = tOrderFroms.get(0);
            upDataOrder(query,tSku);
        }else {
            TOrderFrom tOrderFrom = new TOrderFrom();
            // 订单属性
            tOrderFrom.setType(1);
            // 订单编号
            String uuid = UUID.randomUUID().toString().replace("-", "");
            tOrderFrom.setNo(uuid);
            /// sku信息
            tOrderFrom.setSkuId(Long.valueOf(tSku.getSkuId()));// id
            tOrderFrom.setSkuName(tSku.getSkuName());// name
            tOrderFrom.setSkuNum(tSku.getNum());// num
            // 订单价格
            BigDecimal skuPrice = tSku.getSkuPrice();
            String num = tSku.getNum();
            tOrderFrom.setPrice(skuPrice.multiply(new BigDecimal(num)));
            // 用户信息
            tOrderFrom.setUserId(tSku.getUserId());
            tOrderFrom.setUserName(tSku.getUserName());
            tOrderFromMapper.insertTOrderFrom(tOrderFrom);
        }
    }

    private void upDataOrder(TOrderFrom query, TSku tSku) {
        int skuNum = Integer.parseInt(query.getSkuNum());
        int addNum = Integer.parseInt(tSku.getNum());
        query.setSkuNum(String.valueOf(skuNum+addNum));
        BigDecimal addPrice = tSku.getSkuPrice().multiply(new BigDecimal(addNum));
        query.setPrice(query.getPrice().add(addPrice));
        tOrderFromMapper.updateTOrderFrom(query);
    }
}
